package com.zsrt.oa.order.service.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.zsrt.oa.integral.dao.IntegralDao;
import com.zsrt.oa.integral.dao.impl.IntegralDaoImpl;
import com.zsrt.oa.integral.entity.IntegralEntity;
import com.zsrt.oa.order.dao.OrderDao;
import com.zsrt.oa.order.dao.impl.OrderDaoImpl;
import com.zsrt.oa.order.entity.OrderEntity;
import com.zsrt.oa.order.service.OrderService;
import com.zsrt.oa.user.dao.UserDao;
import com.zsrt.oa.user.dao.impl.UserDaoImpl;
import com.zsrt.oa.user.entity.UserEntity;

/**
 * 订单服务层的实现类
 * @author 罗智壕-13424431416
 *
 * 2017年6月11日
 */
public class OrderServiceImpl implements OrderService {
	
	/**
	 * 初始化订单持久层
	 */
	private OrderDao dao = new OrderDaoImpl();
	
	/**
	 * 初始化用户的持久层
	 */
	private UserDao userdao = new UserDaoImpl();
	
	/**
	 * 初始化积分的持久层
	 */
	private IntegralDao integralDao = new IntegralDaoImpl();
	
	
	/**
	 * 订单的增加
	 * @param userId
	 * @param ticketId
	 * @param map
	 */
	@Override
	public void orderAdd(String price,Object userId, String ticketId, Map<String, Object> map) {
		OrderEntity entity = new OrderEntity();
		String user = String.valueOf(userId);
		entity.setUserId(user);
		entity.setTicketId(ticketId);
		try {
			//订单增加
			int result = dao.orderAdd(entity);
			if(result == 1){
				UserEntity userEntity = new UserEntity();
				userEntity.setUserPhone(user);
				
				int currentOrderId = dao.currentOrderId();
				
				//对用户的金额减少
				int counter = userdao.updateUserMoney(price, userEntity);
				if(counter == 1){
					int discount = userdao.returnUserDiscount(user);
					
					Double over = 0.0;
					if(discount == 0){
						over = (double) (Integer.parseInt(price) * 0.1);
					}else{
						over = (double) (Integer.parseInt(price) * (discount*0.1)) * 0.1;
					}
					
					userEntity.setUserIntegral(over);
					
					//对用户的积分进行增加
					userdao.updateUserintegral(userEntity);
					
					
					IntegralEntity integralEntity = new IntegralEntity();
					integralEntity.setUserId(user);	
					integralEntity.setIgAmount(over);
					integralEntity.setOrderId(currentOrderId);
					
					//对积分表进行增加一条字段
					integralDao.integralAdd(integralEntity);
					map.put("result", "success");
					map.put("message", "增加成功...");
				}
			}else{
				map.put("result", "failure");
				map.put("message", "增加失败...");
			}
		} catch (SQLException e) {
			map.put("result", "error");
			map.put("message", "程序员正在努力抢修中,请稍后再试....");
			e.printStackTrace();
		}
	}
	
	/**
	 * 查询当前用户所有的订单
	 * @param userPhone
	 * @param map
	 * @throws SQLException
	 */
	@Override
	public void myOrderMessage(String userPhone, Map<String, Object> map) {
		List<OrderEntity> list = new ArrayList<OrderEntity>();
		OrderEntity entity = new OrderEntity();
		entity.setUserId(userPhone);
		try {
			list = dao.myOrderMessage(entity);
			map.put("result", "success");
			map.put("message", "查询成功...");
			map.put("data", list);
		} catch (SQLException e) {
			map.put("result", "error");
			map.put("message", "程序员正在努力抢修中,请稍后再试....");
			e.printStackTrace();
		}
	}
}
